/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jonasLab.financistoweb.view.control;

import br.jonasLab.financistoweb.model.Payee;


import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author jonas
 */
@FacesConverter
public class Converter implements javax.faces.convert.Converter{

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        int index = value.indexOf(':');
        if (index != -1) {
            return new Payee(Integer.parseInt(value.substring(0,index)),
            value.substring(index + 1));
        }
        return value;

    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        
        try {
        	Payee optionItem = (Payee) value;
            return optionItem.getId() + ":" +optionItem.getTitle();
        } catch (Exception e) { }
        
        return (String) value;
    }
    
}
